package com.sinoglobal.ningxia.activity.live;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.ab.view.listener.AbOnListViewListener;
import com.ab.view.pullview.AbPullListView;
import com.alibaba.fastjson.JSON;
import com.sinoglobal.networkconnections.ResultCallback;
import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.activity.base.BaseActivity;
import com.sinoglobal.ningxia.adapter.LiveForeShowAdapter;
import com.sinoglobal.ningxia.beans.LiveHomeBean;
import com.sinoglobal.ningxia.downdata.imp.RemoteImpl;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XINDZ on 2017/2/15.
 * 功能:直播预告页面
 */

public class LiveForeshowActivity extends BaseActivity implements AbOnListViewListener {
    List<LiveHomeBean.List1Bean> foreShowtList = new ArrayList();
    private AbPullListView ablvForshow;
    private LiveForeShowAdapter foreShowAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        openChenJInShi();
        setContentView(R.layout.activity_live_foreshow);
        //title
        titleView.setText("直播预告");
        initview();
        loadData();

    }

    private void loadData() {
        //初始化数据
        ResultCallback resultCallback = new ResultCallback(this) {
            @Override
            public void onSuccess(String response) {
                super.onSuccess(response);
                LiveHomeBean liveHomeBean = JSON.parseObject(response, LiveHomeBean.class);
                if (liveHomeBean != null) {
                    int code = liveHomeBean.getCode();
                    int con = liveHomeBean.getCon();//直播预告con是预告条数
                    if (code == 0) {

                        List<LiveHomeBean.List1Bean> list1 = liveHomeBean.getList1();
                        if (list1 != null) {
                            foreShowtList = list1;
                            initAdapter();
                        }

                    } else {
                        showShortToastMessage(liveHomeBean.getMessage());
                    }
                }
            }

            @Override
            public void onErrorCode(Exception e) {
                super.onErrorCode(e);
            }
        };
        RemoteImpl.getInstance().getLiveHome(this).execute(resultCallback);


    }

    private void initAdapter() {
        if (foreShowAdapter == null) {
            foreShowAdapter = new LiveForeShowAdapter(this);
        }
        ablvForshow.setAdapter(foreShowAdapter);
        foreShowAdapter.setForeShowtList(foreShowtList);
    }

    private void initview() {
        ablvForshow = (AbPullListView) findViewById(R.id.ablv_liveforeshow);
        TextView tv1 = (TextView) findViewById(R.id.tv1);
        ablvForshow.getHeaderView().setHeaderProgressBarDrawable(
                this.getResources().getDrawable(R.drawable.progress_circular));
        ablvForshow.setPullLoadEnable(false);
        ablvForshow.setAbOnListViewListener(this);
        TextView tvEmpty = (TextView) findViewById(R.id.tv1);

        tvEmpty.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                loadData();
            }
        });
        ablvForshow.setEmptyView(tvEmpty);
    }

    @Override
    public void onRefresh() {
        ablvForshow.stopRefresh();
        loadData();

    }

    @Override
    public void onLoadMore() {

    }
}
